using System.Collections.Generic;
using UnityEngine;
using Verse;
using RimWorld;
using Verse.Sound;
using UnityEngine.AI;
using System;
using System.Linq;

namespace RimZombieGirl
{
    public static partial class PawnExts
    {
        public static bool IsZombie(this Pawn pawn)
        {
            return pawn?.genes?.HasActiveGene(ZombieDefOf.RI_ZombieCore_Aspect_DeadButNotDead) == true;
        }

        public static DeadNotDead GetZombieGene(this Pawn pawn)
        {
            return pawn?.genes?.GetFirstGeneOfType<DeadNotDead>();
        }
        /// <summary>
        /// 升级僵尸等级
        /// </summary>
        /// <param name="pawn">目标 Pawn</param>
        /// <returns>是否成功升级</returns>
        public static bool UpgradeZombieLevel(this Pawn pawn)
        {
            if (pawn == null || pawn.health == null)
            {
                Log.Error("UpgradeZombieLevel: pawn or pawn.health is null.");
                return false;
            }

            if (!pawn.IsZombie())
            {
                Log.Error($"UpgradeZombieLevel: {pawn.LabelShort} 不是僵尸，无法升级。");
                return false;
            }

            var hediffs = pawn.health.hediffSet;
            if (hediffs == null)
            {
                Log.Error($"UpgradeZombieLevel: {pawn.LabelShort} 的 hediffSet 为空。");
                return false;
            }

            // 获取当前僵尸等级
            int currentIndex = -1;
            Hediff currentHediff = null;

            for (int i = 0; i < RIHediffDefOf.ZombieLevels.Count; i++)
            {
                var def = RIHediffDefOf.ZombieLevels[i];
                var found = hediffs.GetFirstHediffOfDef(def);
                if (found != null)
                {
                    currentIndex = i;
                    currentHediff = found;
                    break;
                }
            }

            // 如果没有任何僵尸等级就1级
            if (currentIndex == -1)
            {
                var firstLevel = RIHediffDefOf.ZombieLevels.First();
                pawn.health.AddHediff(firstLevel);
                Messages.Message($"{pawn.LabelShort} 成为 {firstLevel.label}！", pawn, MessageTypeDefOf.PositiveEvent);
                return true;
            }

            // 已经是最高级
            if (currentIndex >= RIHediffDefOf.ZombieLevels.Count - 1)
            {
                Messages.Message($"{pawn.LabelShort} 已达到最高僵尸等级。", pawn, MessageTypeDefOf.NeutralEvent);
                return false;
            }

            // 升级到下一级
            var nextLevelDef = RIHediffDefOf.ZombieLevels[currentIndex + 1];

            // 移除旧等级
            pawn.health.RemoveHediff(currentHediff);
            pawn.health.AddHediff(nextLevelDef);

            Messages.Message($"{pawn.LabelShort} 升级为 {nextLevelDef.label}！", pawn, MessageTypeDefOf.PositiveEvent);
            return true;
        }
        ///// <summary>
        ///// 升级僵尸等级
        ///// </summary>
        ///// <param name="pawn"></param>
        ///// <param name="level">目标等级</param>
        ///// <param name="branch">进化分支</param>
        //public static bool UpgradeZombieLevel(this Pawn pawn, int level, string branch)
        //{
        //    if (!pawn.IsZombie())
        //    {
        //        Log.Error("Only zombies can upgrade their level.");
        //        return false;
        //    }

        //    if (level <= 0)
        //    {
        //        Log.Error("Level must be greater than 0.");
        //        return false;
        //    }

        //    var gene = pawn.genes?.GetFirstGeneOfType<DeadNotDead>();
        //    if (gene == null)
        //    {
        //        Log.Error("Zombie gene not found.");
        //        return false;
        //    }

        //    // 清除旧的等级
        //    RI_ZombieLevelDef oldLevel = ZombieDefOf.ZombieLevels.GetLevelDef(level: level, branch: branch);
        //    if (!RemoveOldLevel(pawn, oldLevel))
        //    {
        //        Log.Error("Failed to remove old zombie level.");
        //        return false;
        //    }

        //    // 添加新的等级
        //    RI_ZombieLevelDef newLevel = ZombieDefOf.ZombieLevels.GetNextLevelDef(level: level, branch: branch);
        //    if (!AddNewLevel(pawn, newLevel))
        //    {
        //        Log.Error("Failed to add new zombie level.");
        //        return false;
        //    }

        //    // 保存等级信息
        //    gene.SaveLevelInfo(newLevel);

        //    return true;

        //}

        private static bool RemoveOldLevel(this Pawn pawn, RI_ZombieLevelDef def)
        {
            // 移除旧的hediff
            if (def?.hediff != null)
            {
                var hediff = pawn.health.hediffSet.GetFirstHediffOfDef(def.hediff);
                if (hediff != null)
                {
                    pawn.health.RemoveHediff(hediff);
                }
            }
            return true;
        }

        private static bool AddNewLevel(this Pawn pawn, RI_ZombieLevelDef def)
        {
            // 添加新的hediff
            if (def?.hediff != null)
            {
                pawn.health.AddHediff(def.hediff);
            }
            // 设置冥血上限
            var gene = pawn.genes?.GetFirstGeneOfType<DeadNotDead>();
            if (gene != null)
            {
                gene.BloodLimit = def.bloodLimit;
            }
            return true;
        }
    }
}